package com.xtaifghy.wugui.ui;

import com.xtaifghy.wugui.*;
import com.xtaifghy.wugui.GameStateInterface;
import com.xtaifghy.wugui.Resource;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GamePanel extends JPanel {
    GameStateInterface gameState;
    Point[] positions = {
            new Point(50,150),
            new Point(160,115),
            new Point(260,150),
            new Point(17,255),
            new Point(310,255),
            new Point(50,370),
            new Point(160,400),
            new Point(260,370)
    };
    Point[] positions1 = {
            new Point(550,150),
            new Point(660,115),
            new Point(760,150),
            new Point(517,255),
            new Point(810,255),
            new Point(550,370),
            new Point(660,400),
            new Point(760,370)
    };

    private JFrame mainFrame;

    public GamePanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        gameState = new GameState4();
        //gameState.randomGoalState();
        init();
    }

    public void init(){
        setLayout(null);
        ButtonPanel buttonPanel = new ButtonPanel(this);
        add(buttonPanel);
        buttonPanel.setBounds(90,195,165,165);
        buttonPanel.setVisible(true);

        JLabel jLabel1 = new JLabel("当前状态：");
        add(jLabel1);
        jLabel1.setBounds(40,30,200,60);

        JLabel jLabel2 = new JLabel("目标状态：");
        add(jLabel2);
        jLabel2.setBounds(540,30,200,60);

        JTextArea jTextArea = new JTextArea();
        jTextArea.setRows(2);
        add(jTextArea);
        jTextArea.setBounds(130,480,300,80);

        JButton jButton = new JButton("提示");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ArrayList<boolean[]> res = gameState.getPaths();
                String sRes = "";
                if(res.size()==0) sRes = "无解！";
                else{
                    for(int i = 0;i<res.size();i++){
                        sRes=sRes+"第"+(i+1)+"种:";
                        boolean [] actionSeq = res.get(i);
                        for(int j = 0;j< actionSeq.length;j++){
                            if(actionSeq[j]){
                            sRes = sRes + j +",";
                            }
                        }
                        sRes = sRes+"\n";
                    }
                }

                jTextArea.setText(sRes);
            }
        });
        add(jButton);
        jButton.setBounds(50,480,70,30);

        JButton jButton1 = new JButton("重设目标");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameState.randomGoalState();
                repaint();
            }
        });
        add(jButton1);
        jButton1.setBounds(550,480,100,30);

    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D comp = (Graphics2D)g;
        comp.setColor(mainFrame.getContentPane().getBackground());
        comp.fillRect(0,0,this.getSize().width,getSize().height);
        boolean [] curState = gameState.getState();
        for(int i = 0;i < curState.length;i++)
        {
            Image img;
            if(curState[i]) img = Resource.imgOnLight;
            else img = Resource.imgOffLight;
            comp.drawImage(img,positions[i].x,positions[i].y,this);
        }

        curState = gameState.getGoalState();
        for(int i = 0;i < curState.length;i++)
        {
            Image img;
            if(curState[i]) img = Resource.imgOnLight;
            else img = Resource.imgOffLight;
            comp.drawImage(img,positions1[i].x,positions1[i].y,this);
        }
    }

    public void doAction(Integer a){
        gameState.doAction(a);
        repaint();
    }
}
